package com.wx.leetcode;

/**
 * @description: 将每个元素替换为右侧最大元素
 * 输入：arr = [17,18,5,4,6,1]
 * 输出：[18,6,6,6,1,-1]
 * @author: wx
 * @create: 2020-08-18 15:51
 **/
public class ReplaceElementsTopic {

    public static int[] replaceElements(int[] arr) {
        int length = arr.length;
        int[] copyArr = new int[length];
        System.arraycopy(arr, 0, copyArr, 0, length);

        copyArr[length - 1] = -1;

        int temp = arr[length - 1];

        for (int i = length - 2; i > -1; i--) {

            if (temp > copyArr[i]) {
                copyArr[i] = temp;
            } else {
                int b = copyArr[i];
                copyArr[i] = temp;
                temp = b;
            }
        }

        return copyArr;
    }

    public static void main(String[] agrs){
        int[] arr = replaceElements(new int[]{17,18,5,4,6,1});
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

}
